package com.baidu.controller;

import com.baidu.entity.Conmmodity;
import com.baidu.service.ConmmodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @描述 商品业务
 * @Author ConmmodityController
 * @Date 2019/4/18 16:14
 **/
@Controller
@RequestMapping("/conmmodity")
public class ConmmodityController {




    @Autowired
    private ConmmodityService conmmodityService;

    /**
     * 根据id查询图片
     * @param cid
     * @return
     * @throws IOException
     */
    @RequestMapping("/selectById")
    @ResponseBody
   public String selectConmmodityById(Integer cid) throws IOException {
        Conmmodity conmmodity = conmmodityService.selectConmmodityById(cid);
        System.out.println(cid);
        conmmodity.setCimg("images/"+conmmodity.getCimg());
        String img =conmmodity.getCimg();
       if(img.length()>0){
          return img;
       }else{
         return null;
       }
    }
    @RequestMapping("/idone")
    @ResponseBody
    public Conmmodity  selectConmmodityByIdOne(HttpSession session,Integer cid) throws IOException {
       Conmmodity con=conmmodityService.selectConmmodityById(cid);
        con.getCprice();
        con.getCname();
        con.getCid();
        con.setCimg("images/"+con.getCimg());
        session.setAttribute("con",con);
        System.out.println(session.getAttribute("con")+"+++++++++++++++++++++++++++++++");
        if(con!=null){
            return con;
        }else {
            return null;
        }
    }

    /**
     * 查询所有商品信息
     * @return
     */
    @RequestMapping("/selectConmmodity")
    @ResponseBody
    public List<Conmmodity> selectConmmodity(){
        List<Conmmodity> conmmodities = conmmodityService.selectConmmodity();
        List<Conmmodity> con=new ArrayList<>();
        for (Conmmodity conmmodity : conmmodities) {
            Conmmodity conmm=new Conmmodity();
            conmm.setCid(conmmodity.getCid());
            conmm.setCimg("images/"+conmmodity.getCimg());
            conmm.setCname(conmmodity.getCname());
            conmm.setCprice(conmmodity.getCprice());
            con.add(conmm);
        }
        if(con!=null){
            System.out.println(con);
            return con;
        }else{
            return null;
        }

    }
    /**
     * 根据id查询商品信息
     * @param cid
     * @return
     * @throws IOException
     */
    @RequestMapping("/selectAllId")
    @ResponseBody
    public Conmmodity selectConmmodityAllId(Integer cid) throws IOException {
        Conmmodity conmmodity = conmmodityService.selectConmmodityById(cid);
        conmmodity.setCimg("images/"+conmmodity.getCimg());
        System.out.println(conmmodity);
        if(conmmodity!=null){
            return conmmodity;
        }else{
            return null;
        }
    }

    /**
     * 支付进行跳转
     * @return
     */
    @RequestMapping("/cleanMoney")
    @ResponseBody
    public Boolean cleanMoney(Integer  cprice, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(cprice);
        session.setAttribute("GetMoney",cprice);
        if (cprice>=10){
             return true;
        }
        return false;
    }
}
